package cn.loan.tools.message;

import cn.loan.config.key.StatusCode;
import cn.loan.tools.PropertiesTool;
import cn.loan.tools.message.config.Config;
import cn.loan.tools.message.config.HttpUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.net.URLEncoder;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 11:29 2019/3/21
 */
public class SendSMS {


    /**
     * 短信发送接口
     * @param phoneNum  手机号
     * @param code  随机验证码   6位
     * @return  是否发送成功的验证码
     * @throws Exception
     */
    public static String SmsTo(String phoneNum,String code,String key) throws Exception {
        String template= PropertiesTool.getPropertiesContent(key);
        template=template.replace("CODE", code);
        String tmpSmsContent = URLEncoder.encode(template, "UTF-8");;
        String url = Config.BASE_URL + Config.OPERATION;
        String body = "accountSid=" + Config.ACCOUNTSID + "&to=" + phoneNum + "&smsContent=" + tmpSmsContent + HttpUtil.createCommonParam();
        // 提交请求
        String result = HttpUtil.post(url, body);
        System.out.println("result:" + System.lineSeparator() + result);
        JsonNode node = new ObjectMapper().readTree(result);
        String str = node.get("respCode").asText();
        if ("00000".equals(str)){
            return StatusCode.STATUS_SUCCESS;
        }else if("00141".equals(str)) {
            //一小时内操作频繁
            return StatusCode.STATUS_FREQUENTLY;
        }else if ("00142".equals(str)){
            //一天内操作频繁
            return StatusCode.STATUS_LIMIT;
        }else{
            //失败
            return StatusCode.STATUS_FAIL;
        }
    }


}
